*----------------------------------------------------------------------------------------------------------	* 
* RESEARCHERS:		EARN												   									*
* PROGRAMMED BY:	Linea Hasager																			*
* DESCRIPTION:		Binned scatter plots of outcomes and RDD estimates.								 		*
*					1) Load data and create variables														*
*					2) Binned scatterplots (RD plots): outcome against admission month						*
*					3) RD estimatations																		*
*					4) Graph RD estimates								 									*
*					5) Tables with RD estimates																*
*																											*
* CREATED:			Mar. 13, 2018																		   	*
* LAST MODIFIED:	May 25, 2022														       				*
*----------------------------------------------------------------------------------------------------------	*

clear 
set more off

// Directories
global in "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"
global output "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Output"
global temp "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Temp"
global dkdb "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"

*--------------------------------------------------------------------------------------------------------	

******************************
* 1) LOAD DATA
******************************

use "${in}\Analysis_sample.dta", replace


*********************************************
*	2) RDPLOTS: OUTCOMES BY ADMISSION MONTH
*********************************************

global x_time -12 -6 0 6 12


*EARNINGS
preserve 
keep if inrange(m_nr,-12,12)
local var loenmv_13_y_18
bysort m_nr: egen antald=count(pnr) if `var'_sum !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & antald!=.
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var'_sum m_nr if antald > 4, ///
nbins(`min' `max') kernel(triangular) c(0) p(1) ///
graph_options(ylab(0(4)16, format(%10.0fc))  xtitle("Month of Admission") ///
tlabel(${x_time}) ///
graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Thousand USD"))
graph export "${output}\oa_figure4a.png", replace width(5000)
drop antald _min _max w_n
restore



*EMPLOYMENT
preserve 
keep if inrange(m_nr,-12,12)
local var employment_y_18
bysort m_nr: egen antald=count(pnr) if `var'_sum !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & antald!=.
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var'_sum m_nr if antald > 4, ///
nbins(`min' `max') kernel(triangular) c(0) p(1) ///
graph_options(ylab(0(.1).4, format(%10.1fc)) xtitle("Month of Admission") ///
tlabel(${x_time}) ///
graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Average Annual Rate of Employment"))
graph export "${output}\oa_figure4b.png", replace width(5000)
drop antald _min _max w_n
restore


*COMPLEX JOB
preserve 
keep if inrange(m_nr,-12,12)
local var complex_job_y18
bysort m_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & antald!=.
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, ///
nbins(`min' `max') kernel(triangular) c(0) p(1) ///
graph_options(ylab(0(.1).3, format(%10.1fc)) xtitle("Month of Admission") ///
tlabel(${x_time}) ///
graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure4c.png", replace width(5000)
drop antald _min _max w_n
restore


*NON-COMPLEX JOB
preserve 
keep if inrange(m_nr,-12,12)
local var non_complex_job_y18 
bysort m_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & antald!=.
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, ///
nbins(`min' `max') kernel(triangular) c(0) p(1) ///
graph_options(ylab(0.1(.1).4, format(%10.1fc)) xtitle("Month of Admission") ///
tlabel(${x_time}) ///
graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure4d.png", replace width(5000)
drop antald _min _max w_n
restore







****************************************************
*	3) RD ESTIMATIONS
****************************************************



*EMPLOYMENT AND EARNINGS
foreach var in employment_y loenmv_13_y   {
forval i=1/18 {
rdrobust `var'_`i'_sum day_nr,  kernel(tri) c(0) p(1) 
eststo `var'_`i'_sum_e
}
}


*COMPLEX JOB
foreach var in  complex_job_y {
forval i = 1/18 {
rdrobust `var'`i' day_nr,  kernel(tri) c(0)  p(1)
eststo `var'`i'_sum_e
}
}



*FORMAL SKILL UPGRADING IN DENMARK, AGE 18-29
foreach var in udda_dk {
forval i = 1/18 {
rdrobust `var'`i' day_nr if age >=18 & age <=29,  kernel(tri) c(0)  p(1)
eststo `var'`i'_age291
}
}



*MOBILITY
foreach var in moved_new {
forval i=1/18 {
rdrobust `var'_`i' day_nr,  kernel(tri) c(0) p(1) 
eststo `var'_`i'e
}
}




*ANNUAL EFFECTS ON EARNINGS, DISPOSABLE INCOME AND CRIME IN YEAR 1 AND 2, FOR ALL AND BY AGE GROUPS
foreach var in loenmv_13_ dispon_13_ charged convicted property_sm_krsi property_sm_kraf charged_other convicted_other {
forval i=1/2 {
*ALL
rdrobust `var'`i' day_nr,  kernel(tri) c(0)  p(1) 
estadd scalar alpha=e(beta_p_l)[1,1]
eststo `var'`i'_e


*BELOW 25YO, NO CHILDREN
rdrobust `var'`i' day_nr if age25==1,  kernel(tri) c(0)  p(1) 
estadd scalar alpha=e(beta_p_l)[1,1]
eststo `var'`i'_eage251


*ABOVE 25Y0 OR WITH CHILDREN
rdrobust `var'`i' day_nr if age25==0,  kernel(tri) c(0)  p(1) 
estadd scalar alpha=e(beta_p_l)[1,1]
eststo `var'`i'_eage250
}
}


*ANNUAL EARNINGS AND WELFARE BENEFITS
foreach var in loenmv_13 annual_ua {
forval i=1/18 {
rdrobust `var'_`i' day_nr,  kernel(tri) c(0) p(1)
eststo `var'_`i'e
}
}


*ESTIMATES BY INITIAL PLACEMENT
foreach k in urban {
foreach j in 1 0 {

*MOBILITY
local var moved_new
forval i=1/18 {
rdrobust `var'_`i' day_nr if `k'==`j',  kernel(tri) c(0)  p(1)
eststo `var'`i'_`k'`j'
}

}
}



******************************************************
*	4) GRAPHS WITH RD ESTIMATES 
******************************************************

*EARNINGS
local var loenmv_13_y
coefplot (`var'_1_sum_e, aseq(1) \ `var'_2_sum_e, aseq(2) \ `var'_3_sum_e, aseq(3) \ `var'_4_sum_e, aseq(4) \ `var'_5_sum_e, aseq(5) \  `var'_6_sum_e, aseq(6) \ `var'_7_sum_e, aseq(7) \ `var'_8_sum_e, aseq(8) \ `var'_9_sum_e, aseq(9) \ `var'_10_sum_e, aseq(10) \ `var'_11_sum_e, aseq(11) \ `var'_12_sum_e, aseq(12) \ `var'_13_sum_e, aseq(13) \ `var'_14_sum_e, aseq(14) \ `var'_15_sum_e, aseq(15) \ `var'_16_sum_e, aseq(16) \ `var'_17_sum_e, aseq(17) \ `var'_18_sum_e, aseq(18) mcolor(black) ciopts(color(black) )), yline(0, lpattern(dash) lcolor(gs5)) vertical swapnames graphregion(color(white)) xlabel() format(%10.2fc) ytitle("Thousand USD") xtitle("Years Since Admission")  ylab(-2(2)4) 
graph export "${output}\figure1a.png", replace width(5000)



*EMPLOYMENT 
local var employment_y
coefplot (`var'_1_sum_e, aseq(1) \ `var'_2_sum_e, aseq(2) \ `var'_3_sum_e, aseq(3) \ `var'_4_sum_e, aseq(4) \ `var'_5_sum_e, aseq(5) \ `var'_6_sum_e, aseq(6) \ `var'_7_sum_e, aseq(7) \ `var'_8_sum_e, aseq(8) \ `var'_9_sum_e, aseq(9) \ `var'_10_sum_e, aseq(10) \ `var'_11_sum_e, aseq(11) \ `var'_12_sum_e, aseq(12) \ `var'_13_sum_e, aseq(13) \ `var'_14_sum_e, aseq(14) \ `var'_15_sum_e, aseq(15) \ `var'_16_sum_e, aseq(16) \ `var'_17_sum_e, aseq(17) \ `var'_18_sum_e, aseq(18) mcolor(black) ciopts(color(black) )), yline(0, lpattern(dash) lcolor(gs5)) vertical swapnames graphregion(color(white)) xlabel() format(%10.2fc) ytitle("Average Annual Rate of Employment") xtitle("Years Since Admission") ylab(-0.05(0.05)0.1) 
 graph export "${output}\figure1b.png", replace width(5000)
 



*COMPLEX JOB
local var complex_job_y
coefplot (`var'1_sum_e, aseq(1) \ `var'2_sum_e, aseq(2) \ `var'3_sum_e, aseq(3) \ `var'4_sum_e, aseq(4) \ `var'5_sum_e, aseq(5) \ `var'6_sum_e, aseq(6) \ `var'7_sum_e, aseq(7) \ `var'8_sum_e, aseq(8) \ `var'9_sum_e, aseq(9) \ `var'10_sum_e, aseq(10) \ `var'11_sum_e, aseq(11) \ `var'12_sum_e, aseq(12) \ `var'13_sum_e, aseq(13) \ `var'14_sum_e, aseq(14) \ `var'15_sum_e, aseq(15) \ `var'16_sum_e, aseq(16) \ `var'17_sum_e, aseq(17) \ `var'18_sum_e, aseq(18) mcolor(black) ciopts(color(black) )), yline(0, lpattern(dash) lcolor(gs5)) vertical swapnames graphregion(color(white)) xlabel() ytitle("Share") xtitle("Years Since Admission") format(%10.2fc) ylab(-0.05(0.05)0.15) 
graph export "${output}\figure1c.png", replace width(5000)
 




*EDUCATION, AGE 18-29
local var udda_dk
coefplot (`var'1_age291, aseq(1) \ `var'2_age291, aseq(2) \ `var'3_age291, aseq(3) \ `var'4_age291, aseq(4) \ `var'5_age291, aseq(5) \ `var'6_age291, aseq(6) \ `var'7_age291, aseq(7) \ `var'8_age291, aseq(8) \ `var'9_age291, aseq(9) \ `var'10_age291, aseq(10) \ `var'11_age291, aseq(11) \ `var'12_age291, aseq(12) \ `var'13_age291, aseq(13) \ `var'14_age291, aseq(14) \ `var'15_age291, aseq(15) \ `var'16_age291, aseq(16) \ `var'17_age291, aseq(17) \ `var'18_age291, aseq(18) mcolor(black) ciopts(color(black))), yline(0, lpattern(dash) lcolor(gs5)) vertical swapnames graphregion(color(white)) xlabel() ytitle("Share") xtitle("Years Since Admission") format(%10.2fc) ylab(-.0(0.1)0.3) 
graph export "${output}\figure1d.png", replace width(5000)


*MOBILITY
local var moved_new 
coefplot (`var'_1e, aseq(1) \ `var'_2e, aseq(2) \ `var'_3e, aseq(3) \ `var'_4e, aseq(4) \ `var'_5e, aseq(5) \ `var'_6e, aseq(6) \ `var'_7e, aseq(7) \ `var'_8e, aseq(8) \ `var'_9e, aseq(9) \ `var'_10e, aseq(10) \ `var'_11e, aseq(11) \ `var'_12e, aseq(12) \ `var'_13e, aseq(13) \ `var'_14e, aseq(14) \ `var'_15e, aseq(15) \ `var'_16e, aseq(16) \ `var'_17e, aseq(17) \ `var'_18e, aseq(18) mcolor(black) ciopts(color(black) )), yline(0, lpattern(dash) lcolor(gs5)) vertical swapnames graphregion(color(white)) xlabel() format(%10.1fc) ytitle("Share") xtitle("Years Since Admission") ylab(-0.6(0.3)0.3) 
graph export "${output}\figure1e.png", replace width(5000)




*MOBILITY BY INITIAL PLACEMENT
local k urban0
local x urban1
local txt_k "Rural"
local txt_x "Urban"
local var moved_new
coefplot (`var'1_`k', aseq(1) \ `var'2_`k', aseq(2) \ `var'3_`k', aseq(3) \ `var'4_`k', aseq(4) \ `var'5_`k', aseq(5) \ `var'6_`k', aseq(6) \ `var'7_`k', aseq(7) \ `var'8_`k', aseq(8) \ `var'9_`k', aseq(9) \ `var'10_`k', aseq(10) \ `var'11_`k', aseq(11) \ `var'12_`k', aseq(12) \ `var'13_`k', aseq(13) \ `var'14_`k', aseq(14) \ `var'15_`k', aseq(15) \ `var'16_`k', aseq(16) \ `var'16_`k', aseq(16) \ `var'17_`k', aseq(17) \ `var'18_`k', aseq(18) label(`txt_k') msymbol(S) mcolor(black) ciopts(color(black))) (`var'1_`x', aseq(1) \ `var'2_`x', aseq(2) \ `var'3_`x', aseq(3) \ `var'4_`x', aseq(4) \ `var'5_`x', aseq(5) \ `var'6_`x', aseq(6) \ `var'7_`x', aseq(7) \ `var'8_`x', aseq(8) \ `var'9_`x', aseq(9) \ `var'10_`x', aseq(10) \ `var'11_`x', aseq(11) \ `var'12_`x', aseq(12) \ `var'13_`x', aseq(13) \ `var'14_`x', aseq(14) \ `var'15_`x', aseq(15) \ `var'16_`x', aseq(16) \ `var'17_`x', aseq(17) \ `var'18_`x', aseq(18) label(`txt_x')  msymbol(Th) mcolor(black) ciopts(color(black))) , yline(0, lpattern(dash) lcolor(gs5)) vertical swapnames graphregion(color(white)) xlabel() ytitle("Share") xtitle("Years Since Admission")  format(%10.1fc) ylab(-0.6(0.3)0.3) legend(off)
graph export "${output}\figure1f.png", replace width(5000)




**********************************************
*	5) TABLES
**********************************************



*****************************************************************************
* TABLE WITH ESTIMATES FOR RESULTS DISPOSABLE INCOME AND CRIME YEAR 1-2
*****************************************************************************



esttab  loenmv_13_1_e  dispon_13_1_e charged1_e  convicted1_e property_sm_krsi1_e property_sm_kraf1_e charged_other1_e convicted_other1_e using "${output}\table4.tex", replace se(%10.3fc) b(%10.3fc) nolabel varlabel (RD_Estimate "Year 1") noobs nolines nonum nonotes nogaps postfoot("") mgroups(" " "All Crime" "Shoplifting Crime" "Other Crime", pattern(0 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span  erepeat(\cmidrule(lr){@span}) ) prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{1}{cccccccc}}\toprule") mtitles("Earnings" "\shortstack{Disposable \\ Income}" "Charges" "Convictions" "Charges" "Convictions" "Charges" "Convictions") star(* 0.10 ** 0.05 *** 0.01)  posthead(" & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule & \multicolumn{8}{c}{\emph{Panel a. All}} \\")  

esttab  loenmv_13_2_e  dispon_13_2_e charged2_e  convicted2_e property_sm_krsi2_e property_sm_kraf2_e charged_other2_e convicted_other2_e using "${output}\table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Year 2") noobs nolines nonum nonotes nogaps posthead("") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)

esttab loenmv_13_1_eage251 dispon_13_1_eage251 charged1_eage251 convicted1_eage251 property_sm_krsi1_eage251 property_sm_kraf1_eage251 charged_other1_eage251 convicted_other1_eage251 using "${output}\table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Year 1") noobs nolines nonum nonotes nogaps posthead("& & & & & & & & \\ & \multicolumn{8}{c}{\emph{Panel b. Age $<25$ and No Dependents (Not Subject to Welfare Cut)}} \\") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)

esttab loenmv_13_2_eage251 dispon_13_2_eage251 charged2_eage251  convicted2_eage251 property_sm_krsi2_eage251 property_sm_kraf2_eage251 charged_other2_eage251 convicted_other2_eage251 using "${output}\table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Year 2") noobs nolines nonum nonotes nogaps posthead("") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)

esttab loenmv_13_1_eage250 dispon_13_1_eage250 charged1_eage250 convicted1_eage250 property_sm_krsi1_eage250 property_sm_kraf1_eage250 charged_other1_eage250 convicted_other1_eage250 using "${output}\table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Year 1") noobs nolines nonum nonotes nogaps posthead("& & & & & & & & \\ & \multicolumn{8}{c}{\emph{Panel c. Everyone Else (Subject to Welfare Cut)}} \\") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)

esttab loenmv_13_2_eage250 dispon_13_2_eage250 charged2_eage250 convicted2_eage250 property_sm_krsi2_eage250 property_sm_kraf2_eage250 charged_other2_eage250 convicted_other2_eage250 using "${output}\table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Year 2")  noobs nolines nonum nonotes nogaps posthead("") prehead("") postfoot("\bottomrule\end{tabular}}") star(* 0.10 ** 0.05 *** 0.01)


****************************************************
*  ESTIMATED EFFECTS FOR COST-BENEFIT ANALYSIS
****************************************************

* ANNUAL EARNINGS
esttab loenmv_13_1e loenmv_13_2e loenmv_13_3e loenmv_13_4e loenmv_13_5e loenmv_13_6e loenmv_13_7e loenmv_13_8e loenmv_13_9e loenmv_13_10e loenmv_13_11e loenmv_13_12e loenmv_13_13e loenmv_13_14e loenmv_13_15e loenmv_13_16e loenmv_13_17e loenmv_13_18e,   b(%8.3fc) nolabel nomtitle varlabel (RD_Estimate "Annual Earnings") noobs nonotes nostar not nolines 

* ANNUAL WELFARE BENEFITS
esttab annual_ua_1e annual_ua_2e annual_ua_3e annual_ua_4e annual_ua_5e annual_ua_6e annual_ua_7e annual_ua_8e annual_ua_9e annual_ua_10e annual_ua_11e annual_ua_12e annual_ua_13e annual_ua_14e annual_ua_15e annual_ua_16e annual_ua_17e annual_ua_18e, b(%10.3fc) label nomtitle varlabel (RD_Estimate "Annual Welfare Benefits") nonum nonotes nostar not noobs nolines


